﻿using AddrDBContext;
using AddrDBContext.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace AddressKeyBoard
{
    public class DataService : IDisposable
    {
        private AddressDBContext _db = null;

        public bool Connected = false;

        private static string DBFilePath = @"D:\DBFile\StandardAddress.db";

        public List<TStreet> Streets = new List<TStreet>();

        public List<TRoad> Roads = new List<TRoad>();

        public List<TCommunity> Communitys = new List<TCommunity>();

        public List<string> Buildings = new List<string>();

        public List<string> Units = new List<string>();

        public List<string> Floors = new List<string>();

        public List<string> Rooms = new List<string>();

        public string theBuilding = "";

        public string theUnit = "";

        public string theFloor = "";

        public string theRoom = "";

        public TAddress theAddr = null;

        public TStreet theStreet = null;

        public TCommunity theCommunity = null;

        public void OpenDB()
        {
            try
            {
                string DataSouce = "Data Source=" + DBFilePath;
                _db = AddressDBContext.CreatInstance(DataSouce);
                Connected = true;
            }
            catch (Exception ex)
            {
                LogLib.Log.WriteException(ex.ToString());
            }
        }

        public void LoadStreets()
        {
            Streets = _db.TStreets.ToList();
        }

        public void Dispose()
        {
            if(Connected)
            {
                _db.Dispose();
            }
        }

        private void LoadCommunitys(TStreet street)
        {
            var communitys = _db.TCommunitys.Where(r => r.StreetCode == street.Code);
            Communitys = communitys.ToList();

        }

        public bool HasMuchMPH(int selectedIndex, string key)
        {
            int count = _db.TCommunitys.Count(r=>r.Name == key);
            if(count > 1)
            {
                return true;
            }
            return false;
        }

        private void LoadBuildings(TCommunity com)
        {
            var buildings = _db.TAddresss.Where(r => r.Community == com.Code).Select(r => r.Building).Distinct();
            Buildings = buildings.ToList();           
        }

        private void LoadUnits(TCommunity com, string building)
        {
            var units = _db.TAddresss.Where(r => r.Community == com.Code
            && r.Building == building).Select(r => r.Unit).Distinct();
            Units = units.ToList();           
        }

        private void LoadFloors(TCommunity com, string building, string unit)
        {
            var floors = _db.TAddresss.Where(r =>
            r.Community == com.Code
            && r.Building == building
            && r.Unit == unit).Select(r => r.Floor).Distinct();
            Floors = floors.ToList();           
        }

        private void LoadRooms(TCommunity com, string building, string unit, string floor)
        {
            var rooms = _db.TAddresss.Where(r =>
            r.Community == com.Code
            && r.Building == building
            && r.Unit == unit
            && r.Floor == floor).Select(r => r.Room).Distinct();
            Rooms = rooms.ToList();           
        }

        public void SetSelectedValue(CurrentSelectedLevel level, int selectedIndex, string key="")
        {
            switch (level)
            {
                case CurrentSelectedLevel.Streets:
                    theStreet = Streets.Find(r=>r.Code == key);
                    LoadCommunitys(theStreet);
                    break;
                case CurrentSelectedLevel.Communitys:
                    theCommunity = Communitys[selectedIndex];
                    LoadBuildings(theCommunity);
                    break;
                case CurrentSelectedLevel.Buildings:
                    theBuilding = Buildings[selectedIndex];
                    LoadUnits(theCommunity, theBuilding);
                    break;
                case CurrentSelectedLevel.Unit:
                    theUnit = Units[selectedIndex];
                    LoadFloors(theCommunity, theBuilding, theUnit);
                    break;
                case CurrentSelectedLevel.Floor:
                    theFloor = Floors[selectedIndex];
                    LoadRooms(theCommunity, theBuilding, theUnit, theFloor);
                    break;
                case CurrentSelectedLevel.Room:
                    theRoom = Rooms[selectedIndex];
                    var addrs = _db.TAddresss.Where(r => r.Community == theCommunity.Code 
                    && r.Building == theBuilding
                    && r.Unit == theUnit
                    && r.Floor == theFloor
                    && r.Room == theRoom);
                    if (addrs.Count() == 0)
                    {
                        MessageBox.Show("找不到对应的地址.");
                    }
                    theAddr = addrs.First();
                    break;
                default:
                    break;
            }
        }
    }
}
